// SPDX-License-Identifier:GPL-2.0+
/*
 * Copyright (C) 2023, Phytium Technology Co., Ltd.
 * Obtain the board parameter information
 * of Phytium pe2201 board.
 */

#include <common.h>
#include <asm/io.h>
#include <e_uart.h>
#include "parameter.h"
#include "../board_init/board.h"
#include "../cpu.h"

#define FREQ_FROM_SPD_EN_BIT			0

static const board_config_t board_info = {
	.head.magic = PARAMETER_BOARD_MAGIC,
	.head.version = BOARD_VERSION,
	.head.size = BOARD_SIZE,
	.misc_config = (1 << FREQ_FROM_SPD_EN_BIT),
	.power_manager = 0,
	.qspi_freq_rank = 0,
};

const board_config_t *get_board_pm_ptr(uint32_t ver)
{
	board_config_t const *ret;

	ret = (board_config_t *)get_parameter_base(PM_BOARD);

	if (!ret)
		ret = &board_info;
	// If the parameter version is lower than the code version, the default value is returned
	if (ver > ret->head.version)
		ret = NULL;

	return ret;
}
/* Used to distinguish code */
#define DECLARE_PTR(MAJOR, SEC)	board_config_t const * const info_ptr = get_board_pm_ptr(MAJOR << 16 | SEC)

/* 0:disable 1:enable */
uint32_t pm_enable_spd_freq(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return ((info_ptr->misc_config >> FREQ_FROM_SPD_EN_BIT) & 0x1);
}

uint32_t pm_get_power_manager(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return info_ptr->power_manager;
}

uint32_t pm_get_qspi_freq_rank(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return info_ptr->qspi_freq_rank;
}

uint32_t pm_get_param_mio_sel(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return info_ptr->mio_sel;
}

uint32_t pm_get_phy_sel_all(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return info_ptr->phy_sel_mode;
}

uint32_t pm_get_mac_mode_all(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return info_ptr->gmac_speed_mode;
}

uint32_t pm_get_speed_mode_all(void)
{
	DECLARE_PTR(0, 1);

	if (!info_ptr)
		return 0;

	return ((info_ptr->gmac_speed_mode) >> 16);
}
